using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

using Impact.Infrastructure.Plugins;
using Impact.Model.Objects;
using Impact.Model.Service;
using StructureMap;

namespace Impact.Plugins
{
    /// <summary>
    /// Admin interface to configure an instance of the Article Plugin
    /// </summary>
    public class ArticlePluginAdmin : IPluginAdmin
    {
        #region IPluginAdmin Members

        /// <summary>
        /// For a given parameter, creates and returns the proper control used to edit the value.
        /// If there is an existing value, it is passed in and assigned to the new control.
        /// If there is no definition for a particular parameter, default is a textbox.
        /// </summary>
        public Control GetParameterEditControl(PluginParameter parameter)
        {
            Control output;
            switch (parameter.Name)
            {
                case ArticlePlugin.CategoryId:
                    DropDownList dropDown = new DropDownList();
                    ICategoryService service = ObjectFactory.GetInstance<ICategoryService>();
                    ArticleCategoryCollection categories = service.FetchAll(false);
                    dropDown.DataSource = categories;
                    dropDown.DataTextField = ArticleCategory.Columns.Name;
                    dropDown.DataValueField = ArticleCategory.Columns.CategoryId;
                    dropDown.DataBind();

                    if (parameter.Value != null)
                    {
                        dropDown.SelectedValue = parameter.Value;
                    }

                    output = dropDown;
                    break;

                default:
                    TextBox textBox = new TextBox();
                    textBox.Text = parameter.Value;
                    output = textBox;
                    break;
            }
            return output;
        }

        /// <summary>
        /// get the value of the given control
        /// </summary>
        public string GetControlValue(Control control)
        {
            string output = null;
            if (control is DropDownList)
                output = ((DropDownList)control).SelectedValue;
            else if (control is TextBox)
                output = ((TextBox)control).Text;

            return output;
        }


        #endregion
    }
}
